<!DOCTYPE html>
<html lang="zh">

	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>购物车</title>
		<link rel="stylesheet" type="text/css" href="css/index.css" />
		<link rel="stylesheet" type="text/css" href="css/weui.min.css" />
		<link rel="stylesheet" type="text/css" href="css/jquery-weui.min.css" />
	</head>

	<body>
		<div class="schooltop">
			<div class="schooltp">
				<div class="schooltp1">
					<div class="sctp1fl">
						<div class="splogin">
							
						</div>
					</div>
					<div class="sctp1fr">
						<div class="shopcart">
							<a class="nerr" href="shopcart.html" target="_blank">
								<img src="img/shopcrat.png" alt="" class="gwcimg" />
								<span class="gwctxt">我的购物车</span>
							</a>
						</div>
					</div>
				</div>
				<!--<div class="schooltp2">
					<p class="schoolmc">阿克苏地区库车中等职业技术学校--欢迎您</p>
				</div>-->
			</div>

		</div>
		<div class="shopCart">
			<div class="gwcshop">
				<div id='tableHead' class="gwcshop2">
					<div class="gwselect">选择</div>

					<div class="gwcname">
						<p class="gwcpic">商品缩略图</p>
						<p class="gwcitem">商品名称</p>

					</div>
					<div class="guige">规格</div>
					<div class="gwcjg"><span>单价</span></div>
					<div class="gwcnum">数量</div>
					<div class="gwhj">合计</div>
					<div class="detele">操作</div>
				</div>
				<!--<div id="carsub" class="gwcshop1">
					
					<div class="gwselect"><input type="checkbox" name="" id="" value="" /></div>
					
                    	<a href="shop_detail.html" target="_blank" title="切糕新疆纯手工新疆切糕特产美食整块手工玛仁糖零食糕点500g 新疆传统切糕【大块500g*1盒】">

						<div class="gwcname">
							<p class="gwcpic"><img src="//img10.360buyimg.com/cms/s80x80_jfs/t1/117515/35/5149/418660/5eb251edEc05fae81/45ba6f462126332c.jpg" alt="" class="gwcimg1" /></p>
							<p class="gwcitem">切糕新疆纯手工新疆切糕特产美食整块手工玛仁糖零食糕点500g 新疆传统切糕【大块500g*1盒】</p>

						</div>
					</a>
					<div class="guige" title="新疆传统切糕【大块500g*1盒】">新疆传统切糕【大块500g*1盒】</div>
					<div class="gwcjg"><span>￥82.00</span></div>
					<div class="gwcnum">X2</div>
					<div class="gwhj">￥128</div>
					<div class="detele">删除</div>
				</div>
				-->
				
				<div class="jiesuan">
					<div class="jscon">
						<p class="jsconp1" id='selectNum'>已选择0件商品</p>
						<p class="jsconp1"><span class="gwhj1">合计：</span><span class="gwhj1" id="totalMoney">￥0</span></p>
						<p class="jiesuanbtn"><a class="acolor" onclick="settlement()" target="_blank">去结算</a></p>
					</div>
				</div>
			</div>
		</div>
		<div class="shopbottom">
			<div class="shopbmcon">
				<p class="schoolname">
					<a href="http://www.worlduc.com/e/default.aspx?eid=2784009" target="_blank">阿克苏地区库车中等职业技术学校</a>
				</p>
				<p class="schoolname">学校地址：阿克苏地区库车县福鸿路26号</p>
			</div>
		</div>
	</body>

</html>
<script src="js/jquery.min.js"></script>
<script src="js/Global.js"></script>
<script src="js/jquery-1.11.0.js"></script>
<script type="text/javascript" src="js/loginstate.js" ></script>
<script>
	var globalUser = null;
	var selectIdSet =  new Set(); 
jQuery(document).ready(function() {
		getCar();
		deleteCar();
	});
	function getCar(){
		var user = getUserModel();
		globalUser = user;
		$.ajax({
			type: "post",
			url: requesturl +'shopcar/car' ,
			async: false,
			data:{
				'userid':user.id
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				if(data.status == "success") {
					loadCar(data.data);
				} else{
					
				}
			},
			error: function(data) {
				window.location.href = './servererror.html';
			}
		});
	}
	function loadCar(data){
		
		for (var i = 0; i < data.length; i++) {
			var car = data[i];
			var good = getGoodsModel(car.pId);
			var normArr = getNormModel(car.tId);
			
			var content ='<div id="child'+i+'" class="gwcshop1">'+
			'<div class="gwselect" ><input type="checkbox" id="ck'+i+'" onclick="clickA(this)"/></div>'+
					'<a href="shop_detail.html?id='+good.id+'" target="_blank" title="">'+
						'<div class="gwcname">'+
							'<p class="gwcpic"><img id="img'+i+'" src="./goodsimg/'+good.img+'" alt="" class="gwcimg1" /></p>'+
							'<p class="gwcitem" id="content'+i+'" >'+good.content+'</p>'+
						'</div>'+
					'</a>'+
					'<div class="guige" title="">'+normArr[0].typedesc+'</div>'+
					'<div class="gwcjg"><span id="priceA'+i+'" >￥'+good.price+'</span></div>'+
					'<div id="procount'+i+'" class="gwcnum" >X'+car.count+'</div>'+
					'<div class="gwhj" id="total'+i+'">￥'+(car.count)*(good.price)+'</div>'+
					'<input id="good'+i+'" style="display:none" value="'+good.id+'"/>'+
					'<input id="sizesq'+i+'"/ style="display:none" value="'+car.tId+'">'+
					'<div class="detele" id="delete'+i+'">删除</div>'+
					'</div>';
			$('#tableHead').after(content);	
//			var b = '<input type="checkbox" id="ck1" onclick="clickA(this)"/>'+
//				'<input type="checkbox" id="ck2"  onclick="clickA(this)"/>'+
//				'<input type="checkbox" id="ck3"  onclick="clickA(this)"/>'+
//				'<input type="checkbox" id="ck4"  onclick="clickA(this)"/>';
//				$('#tableHead').append(b)
		}
		
		
	}
	function deleteCar(){
		$('.detele').click(function(){
			var id = $(this).attr('id').replace("delete","");
			var child = "#child"+id;
			$(child).hide();
			//减少金额
			var tempTotal ='#total'+id;
		    var total=$(tempTotal).text();
		    total=total.replace("￥","");
		    changeTotal(-total);
		    var userid= globalUser.id;
		    var productId =$('#good'+id).val();
		    var sizeSq = $('#sizesq'+id).val();
		    $.ajax({
			type: "post",
			url: requesturl +'shopcar/delete' ,
			async: false,
			data:{
				'userid':userid,
				'productid':productId,
				'sizesq':sizeSq
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				if(data.status == "success") {
					alert('删除购物车成功');
				} else{
					
				}
			},
			error: function(data) {
				window.location.href = './servererror.html';
			}
		});
		    
		});
	}
	//选择增加，删除减少
	function changeTotal(money){
		var oM=$('#totalMoney').text();
		oM=oM.replace("￥","");
		if(oM==""){
			oM = 0;
		}
		$('#totalMoney').text("￥"+(Number(oM)+Number(money)));
	}
	function clickA(obj){
		//alert("点击了！");
		var id = $(obj).attr('id').replace("ck","");
		var tempTotal ='#total'+id;
		var total=$(tempTotal).text();
		total=total.replace("￥","");
		var tempId ="#ck"+id 
		var selectNum=$('#selectNum').text();
		var num =  selectNum.replace(/[^0-9]/ig,"");
		if (!$(tempId).get(0).checked) {
			selectIdSet.delete(id);
			total = -total;
			num = Number(num)-1;
      }else{
       	   selectIdSet.add(id);
       	   num = Number(num)+1;
       }
        $('#selectNum').text('已选择'+num+'件商品');
		changeTotal(total);
	}
	function settlement(){
		localStorage.setItem("shopcardata",null);
		if(selectIdSet.size==0){
			alert('未选择商品！');
		}
		var dataA = new Array();
		var index = 0;
		for (var id of selectIdSet) {
			var obj = new Object();
		    obj.total = $('#totalMoney').text().replace("￥","");
		    obj.user = globalUser;
		    var goods = new Object();
		   var productId =$('#good'+id).val();
		   var sizeSq = $('#sizesq'+id).val();
		   obj.goodsid = productId;
		   obj.sizesq=sizeSq;
		   obj.count = $('#procount'+id).text().replace("X","");
		   obj.price = $('#priceA'+id).text().replace("￥","");
		   obj.money=$('#total'+id).text().replace("￥","");
		   obj.img=$('#img'+id).attr('src');
		   obj.content = $('#content'+id).text();
		   dataA[index]=obj;
		   index++;
		}
		localStorage.setItem("shopcardata",JSON.stringify(dataA));
		window.open("./jiesuan.html");
	}
</script>